package org.ChrisRiffle.Acme.BoundingBox.controlers;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;

import org.ChrisRiffle.Acme.BoundingBox.ui.BoxQueryPanel;

/**
 * 
 * Coordinates communication with the Query Panel
 * 
 * @author RIF
 * 
 */
public enum QueryPanelControler {

	// singleton
	INSTANCE;

	private BoxQueryPanel boxQuerySwingPanel;

	private QueryPanelListener panelListener;

	
	public enum TXT_FIELD_TYPE {

		leftX, rightX, upperY, lowerY
	}

	/**
	 * Gets the boxQuerySwingPanel
	 * 
	 * @return the boxQuerySwingPanel
	 */
	public BoxQueryPanel getBoxQuerySwingPanel() {
		
		if (boxQuerySwingPanel == null) {
			
			boxQuerySwingPanel = new BoxQueryPanel();
			
			config(boxQuerySwingPanel);
		}
		return boxQuerySwingPanel;
	}
	
	
	
	/**
	 * Gets left x
	 * @return left x
	 */
	public Float getLX(){
		return QueryPanelControler.INSTANCE.parseTxtField(TXT_FIELD_TYPE.leftX);
	}
	
	
	/**
	 * Gets right x
	 * @return right x
	 */
	public Float getRX(){
		
		return QueryPanelControler.INSTANCE.parseTxtField(TXT_FIELD_TYPE.rightX);
	}
	
	/**
	 * Gets upper Y
	 * @return upper Y
	 */
	public Float getUY(){
		return QueryPanelControler.INSTANCE.parseTxtField(TXT_FIELD_TYPE.upperY);
		
	}
	
	/**
	 * Gets lower Y
	 * @return lower Y
	 */
	public Float getLY(){
		return QueryPanelControler.INSTANCE.parseTxtField(TXT_FIELD_TYPE.lowerY);
		
	}
	
	/**
	 * Configs the panels components(mostly plumbing)
	 * 
	 */
	private void config(BoxQueryPanel pPanel) {
		
		configRunQueryButton(pPanel.getRunQueryButton());
	}
	
	

	/**
	 * Parses TxtField
	 * 
	 * @param txtFieldType
	 * 
	 * @return TxtField
	 */
	public Float parseTxtField(TXT_FIELD_TYPE txtFieldType){
		
		JFormattedTextField tF = null;
		
		if(txtFieldType == TXT_FIELD_TYPE.leftX){
			
			tF = getBoxQuerySwingPanel().getLeftXTxtField();
			
		}else if(txtFieldType == TXT_FIELD_TYPE.rightX){
			
			tF = getBoxQuerySwingPanel().getRightXTxtField();
			
		}else if(txtFieldType == TXT_FIELD_TYPE.upperY){
			
			tF = getBoxQuerySwingPanel().getUpperYTxtField();
			
			
		}else if(txtFieldType == TXT_FIELD_TYPE.lowerY ){
			
			tF = getBoxQuerySwingPanel().getLowerYTxtField();
			
		}
		
		
		String txt = tF.getText();
		
		Float asFloat = null;
		
		try {
			
			asFloat = Float.parseFloat(txt);
			
			
		} catch (Exception e) {
			
			asFloat = null;
			
		}
		
		return asFloat;
		
	}
	
	

	/**
	 * configRunQueryButton
	 * 
	 * @param pButton
	 */
	private void configRunQueryButton(JButton pButton){
		
		pButton.setActionCommand(QueryPanelListener.RUN_BUTTON_CLICK);
		
		pButton.addActionListener(getPanelListener());				
	}
	
	
	
	/**
	 * gets the QueryPanelListener
	 * 
	 * @return QueryPanelListener
	 */
	private QueryPanelListener getPanelListener(){
		
		if(panelListener == null){
			panelListener = new QueryPanelListener();
		}
		return panelListener;
		
	}
	
	
	
}



